package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_SpecialDao;
import com.hhit.collaborative_learning.dao.impl.BBS_SpecialDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;

/**
 * Servlet implementation class GetBBSSpecialServlet
 */
@WebServlet("/community/GetBBSSpecialServlet")
public class GetBBSSpecialServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetBBSSpecialServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		BBS_SpecialDao bsDao=new BBS_SpecialDaoImpl();
		JSONObject jobj=new JSONObject();
		Connection con=DBAccess.getInstance().getConn();
		ResultSet rs=bsDao.queryBBS_Special(con, 0, 0, null, null, null, null);
		try {
			JSONArray array=JsonUtil.formatRsToJsonArray(rs);
			if(rs!=null){
				jobj.put("isSuccess", "true");
			}else{
				jobj.put("isSuccess", "false");
			}
			jobj.put("content", array.toString());
			ResponseUtil.write(response, jobj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("respons object :"+jobj.toString());
		}finally{
			DBAccess.getInstance().close(con, null, rs);
			System.out.println("respons object :"+jobj.toString());
		}
	}

}
